package com.jess.arms.widget.onchangeview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jess.arms.R;

import com.jess.arms.callback.EmptyCallback;
import com.jess.arms.callback.ErrorCallback;
import com.jess.arms.callback.LoadSirManagement;

import com.jess.arms.callback.LottieLoadingCallback;
import com.jess.arms.callback.TimeoutCallback;

import com.jess.arms.mvp.IView;

import com.jess.arms.widget.LoadingView;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;

/**
 * ================================================
 * author : Alex
 * version:
 * Description:
 * <p>
 * Created by ErrorBook on 2021/1/5.
 * ================================================
 */
public class OnChangeLayoutView extends FrameLayout  {

    private final Context mContext;
    private FrameLayout mContentLayoutView;
    private LoadService mBaseLoadService;
    private LoadingView loadingView;

    private IChangeLayoutViewInterfaces mChangeViewInterfaces;
    private RelativeLayout mRlTitle;
    /**
     * 判断是否是显示标题
     */
    private boolean isShowTitle = false;

    public OnChangeLayoutView(@NonNull Context context) {
        this(context, null);
    }

    public OnChangeLayoutView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("CustomViewStyleable")
    public OnChangeLayoutView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    public void isShowTitle(boolean isShowTitle) {
        this.isShowTitle = isShowTitle;
    }

    /**
     * 初始化View
     */
    private void initView() {
        View rootView = inflate(mContext, R.layout.public_onchage_layout_view, this);
        mContentLayoutView = rootView.findViewById(R.id.common_fl_content);
        mRlTitle = rootView.findViewById(R.id.rl);

        if (isShowTitle) {
            //初始化title
            initTitle(rootView);
            //处理返回按钮
            initBack(rootView);
            //初始化右边按钮逻辑
            initRight(rootView);
        }
    }

    /**
     * 添加布局
     * @param content    activity 布局 或者 fragment 布局
     */
    public void setContentView(View content) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        if (mContentLayoutView != null) {
            mContentLayoutView.addView(content, params);
        }
        if (mRlTitle!=null){
            mRlTitle.setVisibility(isShowTitle?View.VISIBLE:View.GONE);
        }
        mBaseLoadService = LoadSirManagement.initActivityLoadSir().register(mContentLayoutView, (Callback.OnReloadListener) this::onNetReload);
    }
    public void setOnNetReload(IChangeLayoutViewInterfaces iChangeLayoutViewInterfaces) {
        mChangeViewInterfaces = iChangeLayoutViewInterfaces;
    }

    /**
     * 初始化title
     * @param rootView
     */
    private void initTitle(View rootView) {
        TextView tvTitleTitle = rootView.findViewById(R.id.tv_title_title);
        tvTitleTitle.setText(mChangeViewInterfaces.getContentTitle() == null ? "" : mChangeViewInterfaces.getContentTitle());
    }

    /**
     * 初始化右边按钮逻辑
     *
     * @param rootView
     */
    private void initRight(View rootView) {
        TextView tv_title_right = rootView.findViewById(R.id.tv_title_right);
        tv_title_right.setVisibility(mChangeViewInterfaces.isUseRight() ? View.VISIBLE : View.GONE);
        tv_title_right.setText(mChangeViewInterfaces.getRightString() == null ? "" : mChangeViewInterfaces.getRightString());
        tv_title_right.setOnClickListener(v -> mChangeViewInterfaces.onRightClick());
    }

    /**
     * 处理返回按钮
     *
     * @param rootView
     */
    private void initBack(View rootView) {
        LinearLayout llTitleBack = rootView.findViewById(R.id.ll_title_back);
        llTitleBack.setVisibility(mChangeViewInterfaces.isUseBack() ? View.VISIBLE : View.GONE);
        llTitleBack.setOnClickListener(v -> mChangeViewInterfaces.onBackClick());
    }


    /**
     * 重新刷新数据
     *
     * @param view
     */
    private void onNetReload(View view) {
        mChangeViewInterfaces.onNetReload(view);
    }

    /**
     * 加载布局
     */
    public void showLoadingLayout() {
        if (mBaseLoadService != null) {
            mBaseLoadService.showCallback(LottieLoadingCallback.class);
        }
    }

    /**
     * 显示空白页面
     */
    public void showEmpty() {
        if (mBaseLoadService != null)
            mBaseLoadService.showCallback(EmptyCallback.class);
    }

    /**
     * 显示内容
     */
    public void showContent() {
        if (mBaseLoadService != null) {
            mBaseLoadService.showSuccess();
        }
    }

    /**
     * 显示加载失败
     */
    public void showFailure() {
        if (mBaseLoadService != null) {
            mBaseLoadService.showCallback(ErrorCallback.class);
        }
    }

    /**
     * 显示无网络
     */
    public void showNoNetwork() {
        if (mBaseLoadService != null) {
            mBaseLoadService.showCallback(TimeoutCallback.class);
        }
    }

    /**
     * 加载超时
     */
    public void showTimeOut() {
        if (mBaseLoadService != null) {
            mBaseLoadService.showCallback(TimeoutCallback.class);
        }
    }


    /**
     * 隐藏
     */
    public void closeLoadingDialog() {
        if (loadingView != null)
            loadingView.dismiss();
    }

    /**
     * 显示
     */
    public void showLoadingDialog() {
        if (loadingView == null)
            loadingView = new LoadingView(mContext, R.style.CustomDialog);
        loadingView.show();

    }

    /**
     * 隐藏加载框
     */
    public void hideLoading() {
        closeLoadingDialog();
    }

    /**
     * 显示加载框
     */
    public void showLoading() {
        showLoadingDialog();
    }



}
